package org.cqut.system.notice.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cqut.framework.utils.DateUtils;
import org.cqut.system.notice.domain.Notice;
import org.cqut.system.notice.service.NoticeService;
import org.cqut.system.notice.mapper.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author XiAoXu
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
        implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public List<Notice> getNoticeList(String courseId) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        return list(queryWrapper);
    }

    @Override
    public boolean publishNotice(String noticeId) {
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("notice_id", noticeId);
        updateWrapper.set("status", 2);
        return update(updateWrapper);
    }

    @Override
    public void noticeIsEnd() {
        //查出所有通知(只包含已发布的通知)
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 2);
        List<Notice> list = list(queryWrapper);
        //判断时间
        for (Notice notice : list) {
            if (DateUtils.isFinished(notice.getNoticeEndTime()) > 0) {
                UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("notice_id", notice.getNoticeId());
                updateWrapper.set("status", 3);
                //结束则更改状态
                update(updateWrapper);
            }
        }
    }

    @Override
    public List<Notice> getNoticeListForStudent(String courseId) {
        //学生只能看已发布的通知,且开始时间是今天
        return noticeMapper.selectListForStudent(courseId);
    }
}




